package com.gemme.gemme.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.gemme.gemme.R;

public class GmWebView extends RelativeLayout {
    private Context context;
    Button button1;
    public GmWebView(Context context) {
        this(context,null);
    }

    public GmWebView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public GmWebView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        initView();
    }
    WebView webView;
    private void initView() {
        LayoutInflater.from(context).inflate(R.layout.gmwebview,this);

        webView = findViewById(R.id.webview);
        WebSettings webSettings = webView.getSettings();

        // step 4: bind javascript
        // note:if api level lower than 17(android 4.2), addJavascriptInterface has security
        // issue, please use x5 or see https://developer.android.com/reference/android/webkit/
        // WebView.html#addJavascriptInterface(java.lang.Object, java.lang.String)
        webSettings.setJavaScriptEnabled(true);
        webView.removeJavascriptInterface("searchBoxJavaBridge_");

        // init webview settings
        webSettings.setAllowContentAccess(true);
        webSettings.setDatabaseEnabled(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setAppCacheEnabled(true);
        webSettings.setSavePassword(false);
        webSettings.setSaveFormData(false);
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
        //访问网页 todo 直接写会调用手机里面的浏览器
//        webView.loadUrl("https://api.skinrun.cn/html/ai/index.html?token=qpIT382W2TpJopgqmi9Ejmhc3fchCjWwi%2F31kAdmNTQqzAhw2URgdw");
        webView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });

//        webView.loadUrl("https://api.skinrun.cn/html/ai/index.html?token=qpIT382W2TpJopgqmi9Ejmhc3fchCjWwi%2F31kAdmNTQqzAhw2URgdw");
    }

    public void setUrl(String url){
        webView.loadUrl(url);
    }

}
